<?php

namespace api\modules\app\controllers;

use api\components\BaseController;
use api\components\Request;
use api\components\UploadExcelFile;
use api\modules\app\models\MetaArea;
use api\modules\app\models\Qrcode;
use api\modules\app\models\Unit;
use api\modules\app\services\CacheService;
use api\modules\app\services\ImportUnitService;
use api\modules\app\services\UnitService;
use api\traits\ControllerTrait;
use app\components\Job;
use Yii;

class UnitController extends BaseController
{
    use ControllerTrait;

    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'],$actions['create'],$actions['update'],$actions['delete'],$actions['view']);
        return $actions;
    }

    public function actionIndex()
    {
        $params = $this->getQuery();
        return UnitService::search($params);
    }

    public function actionSnycGeo()
    {
        return UnitService::translateToGcj();
    }

    public function actionSearch()
    {
        $params = $this->getQuery();
        return UnitService::searchFromDC($params);
    }

    public function actionView()
    {
        $unitCode = $this->getQuery('id');
        return UnitService::view($unitCode);
    }

    public function actionCreate()
    {
        $params = $this->getParams();
        $user = $this->getUser();
        return UnitService::create($user,$params);
    }

    public function actionUpdate()
    {
        $unitCode = $this->getQuery('id');
        $params = $this->getParams();
        $user = $this->getUser();
        return UnitService::update($unitCode,$params,$user);
    }

    public function actionDelete()
    {
        $user = $this->getUser();
        $unitCode = $this->getQuery('id');
        return $result = UnitService::delete($user, $unitCode);
    }

    public function actionExport()
    {
        $params = $this->getQuery();
        return UnitService::export($params);
    }

    public function actionExportLocation() {
        $params = $this->getQuery();
        return UnitService::exportLocation($params);
    }

    public function actionAudit()
    {
        $params = $this->getParams();
        return UnitService::audit($params);
    }

    public function actionUnbind()
    {
        $code = $this->getParams('unit_code');
        return UnitService::unbind($code);
    }

    public function actionBatchTag()
    {
        $params = $this->getParams();
        return UnitService::batchTag($params);
    }

    public function actionTest() {
      print_r($this->getUser());exit;
        try {
            $token = $this->getToken();
            $user = (new CacheService())->getUserInfoByToken($token);
            if (empty($user)) {
                $headers = Yii::$app->request->headers;
                $user = Yii::$app->userCenter->loginByTokenNew($token);
                print_r($user);exit;
                if (!empty($user)) {
                    (new CacheService())->cacheUserInfoByToken($token, $user);
                }
            }
            return $user;
        }catch (\Exception $e){
            return $user = [
                'id' => 1,
                'username' => 'admin'
            ];
        }
    }

    public function actionImportBak() {
        $params = $this->getParams();
        //设施编码 设施名称 管理部门 二级管理部门 所属区 所属街道 设施位置 经度（wgs84） 纬度（wgs84）
        $uploadModel = new UploadExcelFile();
        $res = $uploadModel->upload();
        if($res !== true){
            throw new \Exception('文件上传失败');
        }
        $data = $uploadModel->readFile();

        $service = new ImportUnitService();
        $result = $service->import($data,$params);

        return $result;
    }

    public function actionImportFire() {
        //消火栓
        $uploadModel = new UploadExcelFile();
        $res = $uploadModel->upload();
        if($res !== true){
            throw new \Exception('文件上传失败');
        }
        $data = $uploadModel->readFile();

        $service = new ImportUnitService();
        $result = $service->importFire($data);

        return $result;
    }

    public function actionImport() {
        //消火栓
        $service = new ImportUnitService();
        $uploadModel = new UploadExcelFile();
        $res = $uploadModel->upload();
        if($res !== true){
            throw new \Exception('文件上传失败');
        }
        $data = $uploadModel->readFile();
        $result = $service->importCityFire($data);

        return $result;
    }


}

